package com.zhuliwei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuliwei.domain.dto.NoticeDto;
import com.zhuliwei.domain.entity.Notice;
import com.zhuliwei.domain.vo.NoticeVo;
import com.zhuliwei.domain.vo.PageVo;
import com.zhuliwei.mapper.ArticleMapper;
import com.zhuliwei.mapper.NoticeMapper;
import com.zhuliwei.mapper.UserMapper;
import com.zhuliwei.service.NoticeService;
import com.zhuliwei.utils.BeanCopyUtils;
import com.zhuliwei.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * (Notice)表服务实现类
 *
 * @author 朱力维
 * @since 2024-11-14 10:56:52
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;


    @Override
    public List<Notice> getNoticesByUserId() {
        String myId = SecurityUtils.getUserId();
        LambdaQueryWrapper<Notice> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Notice::getCreateTime);
        lqw.eq(Notice::getUserId, myId);
        List<Notice> notices = list(lqw);
        return notices;
    }

    @Override
    public PageVo pageNoticeList(Integer pageNum, Integer pageSize, NoticeDto noticeDto) {
        //分页查询
        LambdaQueryWrapper<Notice> lqw = new LambdaQueryWrapper();
        lqw.like(StringUtils.hasText(noticeDto.getContent()), Notice::getContent, noticeDto.getContent());
        lqw.eq(StringUtils.hasText(noticeDto.getType()), Notice::getType, noticeDto.getType());
        lqw.eq(StringUtils.hasText(noticeDto.getIsRead()), Notice::getIsRead, noticeDto.getIsRead());
//        lqw.eq(StringUtils.hasText(noticeDto.getUserId()), Notice::getUserId, noticeDto.getUserId());
//        lqw.eq(null != noticeDto.getCreateTime(), Notice::getCreateTime, noticeDto.getCreateTime());
        // 拼接sql语句，将数据库的DateTime格式转换成Date格式进行比较
        lqw.apply(null != noticeDto.getCreateTime(), "DATE(create_time) = {0}", noticeDto.getCreateTime());
        lqw.orderByDesc(Notice::getCreateTime);
        Page<Notice> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, lqw);
        List<Notice> notices = page.getRecords();
        List<NoticeVo> noticeVos = BeanCopyUtils.copyBeanList(notices, NoticeVo.class);
        noticeVos.forEach(noticeVo -> {
            noticeVo.setUserName(userMapper.selectById(noticeVo.getUserId()).getUserName());
            if (StringUtils.hasText(noticeVo.getArticleId())&&!noticeVo.getArticleId().equals("0")) {
                noticeVo.setArticleName(articleMapper.selectById(noticeVo.getArticleId()).getTitle());
            } else if (StringUtils.hasText(noticeVo.getArticleId()) && noticeVo.getArticleId().equals("0")) {
                noticeVo.setArticleName("留言");
            }
        });
        //封装数据返回
        PageVo pageVo = new PageVo(noticeVos, page.getTotal());
        return pageVo;
    }
}

